package com.kongshidian.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kongshidian.App;
import com.kongshidian.R;
import com.kongshidian.bean.ProductGroup;
import com.kongshidian.ui.ProductDetailActivity;
import com.kongshidian.ui.ProductPromotionsActivity;
import com.kongshidian.ui.view.AbsViewHolderAdapter;
import com.kongshidian.ui.view.HorizontalListView;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author fbl
 * 商品组合购
 */
public class ProductGroupFragment extends BaseFragment{
	
	RelativeLayout mEmptyRelat;
	ListView mProductBundleList;
	private ProductGroupAdapter mAdapter;
	private List<ProductGroup> mProductGroups = new ArrayList<ProductGroup>();
	private TextView mEmptyText;
	
	@Override  
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
			Bundle savedInstanceState) { 

		return inflater.inflate(R.layout.fragement_product_group, container, false);  
	}  

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		if(ProductPromotionsActivity.mGroups == null  ){
			mProductGroups = new ArrayList<ProductGroup>();
		}else {
			mProductGroups = ProductPromotionsActivity.mGroups;
			if(mProductGroups.size() > 0){
				mProductGroups.get(0).setShow(true); //设置第一行展开
			}
			
		}
		
		
		mEmptyRelat = (RelativeLayout) getView().findViewById(R.id.empty_relat);
		mEmptyText = (TextView) getView().findViewById(R.id.empty_text);
		mEmptyText.setText(R.string.no_product_groups);
		mProductBundleList = (ListView) getView().findViewById(R.id.product_group_list);
		mProductBundleList.setEmptyView(mEmptyRelat);
		mAdapter = new ProductGroupAdapter(getActivity(), R.layout.item_product_bundle);
		mProductBundleList.setAdapter(mAdapter);
		
		mAdapter.update(mProductGroups);
		
		mProductBundleList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent,
					View view, int position, long id) {
				
				for (int i = 0; i < mProductGroups.size(); i++) {
					if(position != i ){
						mProductGroups.get(i).setShow(false);	
					}
				}
				if( mAdapter.getData().get(position).isShow() ){
					
					mProductGroups.get(position).setShow(false);
				}else {
					mProductGroups.get(position).setShow(true);
					
				}
				mAdapter.update(mProductGroups);
				
			}
			
		});
	}


	public class ProductGroupAdapter extends AbsViewHolderAdapter<ProductGroup>{

		public ProductGroupAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(final int pos, final ProductGroup itemData) {

			LinearLayout mSimpleLin  = getViewFromHolder(R.id.item_productbbudle_lin);
			LinearLayout mDetailLin = getViewFromHolder(R.id.item_productbbudle_detail_lin);
			LinearLayout mDetailListLin = getViewFromHolder(R.id.item_productbbudle_detail_list_lin);
			
			TextView mProGroupPrice = getViewFromHolder(R.id.item_productbundle_price_text);
			TextView mProGroupPrice1 = getViewFromHolder(R.id.item_productbundle_detail_price_text);
			TextView mProGroupMarkerPrice = getViewFromHolder(R.id.item_productbundle_detail_price_text1);
			TextView mSaveText = getViewFromHolder(R.id.item_productbundle_detail_save_text);
			
			mProGroupPrice.setText("￥"+itemData.getPrice());
			mProGroupPrice1.setText("￥"+itemData.getPrice());
			mProGroupMarkerPrice.setText("￥"+(itemData.getPrice()+600));
			mProGroupMarkerPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			
			mSaveText.setText("立省"+600);
			
			
			TextView mNameText =  getViewFromHolder(R.id.item_productbundle_name_text);
			TextView mDetailNameText =  getViewFromHolder(R.id.item_productbundle_detail_name_text);
			Button mbut  = getViewFromHolder(R.id.item_productBundle_detail_button);
			
			mNameText.setText(itemData.getName());
			mDetailNameText.setText(itemData.getName());
			
			
			if( !itemData.isShow() ){
				mSimpleLin.setVisibility(View.VISIBLE);
				mDetailLin.setVisibility(View.GONE);
				
				List<String> imagePath = new ArrayList<String>(); 
				for (int i = 0; i < itemData.getProductDTO().size(); i++) {
						imagePath.add(itemData.getProductDTO().get(i).getImage());
				}
				
				ProductGroupHorizonAdapter mProductGroupHorzinAdapter = new ProductGroupHorizonAdapter(getActivity(), R.layout.item_order_manager_double_image);
				HorizontalListView hoListView = getViewFromHolder(R.id.product_bundle_image_list);
				hoListView.setAdapter(mProductGroupHorzinAdapter);
				mProductGroupHorzinAdapter.update(imagePath);
				
			}else {
				mSimpleLin.setVisibility(View.GONE);
				mDetailLin.setVisibility(View.VISIBLE);
				
				mDetailListLin.removeAllViews();
				
				for (int i = 0; i < itemData.getProductDTO().size(); i++) {
					LinearLayout convertView = (LinearLayout) LayoutInflater.from(getActivity())
							.inflate(R.layout.item_product_bundle_detail_list, null);
//					RelativeLayout mRelat = (RelativeLayout) convertView.findViewById(R.id.item_productbundle_detail_relat_list);
					ImageView mProductImage = (ImageView) convertView.findViewById(R.id.item_productbundle_detail_list_image);
					TextView mText = (TextView) convertView.findViewById(R.id.item_productbundle_detail_list_text);
					
					final int j = i;
					convertView.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View view) {	
							long productID = itemData.getProductDTO().get(j).getId();
							Intent intent = new Intent(getActivity(),ProductDetailActivity.class);
							intent.putExtra("productID", productID+"");
							startActivity(intent);
						}
					});
					
					mDetailListLin.addView(convertView);
					ImageLoader.getInstance().displayImage(itemData.getProductDTO().get(i).getImage(), mProductImage);
					mText.setText(itemData.getProductDTO().get(i).getName());
					
				}
				
			}
			
			mbut.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View paramView) {
					
					
					App.showShortToast("pos=="+pos);
				}
			});
			
		
		}
		
	}
	
	/** 多个商品下横向图片滑动 */
	public  class ProductGroupHorizonAdapter extends AbsViewHolderAdapter<String>{
		public ProductGroupHorizonAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, String itemData) {
			ImageView mImg = getViewFromHolder(R.id.item_order_manager_hon_img);
			ImageLoader.getInstance().displayImage(itemData.toString(), mImg);
		}
	}


}
